var models = require('../db')
var express = require('express')
var router = express.Router()
var mysql = require('mysql')
var $sql = require('../sqlMap')

var conn = mysql.createConnection(models.mysql)

conn.connect()
var jsonWrite = function (res, ret) {
    if (typeof ret === 'undefined') {
        res.json({
        code: '1',
        msg: '操作失败'
        })
    } else {
        res.json(ret)
    }
}

router.post('/login',(req,res) =>{
    var sql = $sql.admin.get
    var params = req.body
    var list=[];
    let success = false;
    console.log(params)
    conn.query(sql, [], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            console.log(result)
            list = result
            for(let admin of list){
                if(admin.admin_name==params.username&&admin.admin_password==params.password){
                    res.send(JSON.stringify({
                        msg:"操作成功",
                        status:200,
                        admin:admin
                    }))
                    return;
                }else if(admin.admin_name!=params.username&&admin.admin_password==params.password||
                    admin.admin_name==params.username&&admin.admin_password!=params.password){
                        res.send(JSON.stringify({
                            msg:"账号或密码错误",
                            status:402,
                        }))
                        return;
                }
            }
            res.send(JSON.stringify({
                msg:"用户不存在",
                status:402,
            }))
        }
    })
})


router.get('/getAdminList',(req,res) =>{
    var sql = $sql.admin.get
    var params = req.query
    console.log(params)
    conn.query(sql, [], function (err, result) {
        if (err) {
        console.log(err)
        }
        if (result) {
        console.log(result)
        res.send(result)
        }
    })
})

router.post('/addAdmin',(req,res)=>{
    var sql = $sql.admin.add
    var params = req.body;
    params.create_time = new Date();
    params.modify_time = new Date();
    params.admin_type = 1;
    console.log(params)
    conn.query(sql, [params], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWrite(res,result)
        }
    })
})

router.post('/editAdmin',(req,res)=>{
    var sql = $sql.admin.update
    var params = req.body;
    params.admin_id = parseInt(params.admin_id);
    params.admin_type = parseInt(params.admin_type);
    params.is_del = 0;
    params.create_time = new Date(params.create_time);
    params.modify_time = new Date();
    console.log(params)
    conn.query(sql, [params,params.admin_id], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWrite(res,result)
        }
    })
})

router.post('/deleteAdmin',(req,res)=>{
    var sql = $sql.admin.update
    var params = req.body
    params.is_del = 1;
    params.modify_time = new Date();
    console.log(params)
    conn.query(sql, [params,params.admin_id], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            jsonWrite(res,result)
        }
    })
})


module.exports = router;